//index.js
//获取应用实例
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
// 语言
var util = require('../../utils/util.js')
import event from '../../utils/event'

Page({
  data: {
    share_img:{},
    statusBarHeight: 20,
    titleBarHeight: 44,
    totals:1,
    autoplay: true,
    interval: 10000,
    duration: 500,
    goodsDetail: {},
    swiperCurrent: 0,
    hasMoreSelect: false,
    selectSize: "",
    selectSizePrice: 0,
    shopNum: 0,
    //语言 - begin
    language: '',
    //语言 - end
    hideShopPopup: true,
    buyNumber: 0,
    buyNumMin: 1,
    buyNumMax: 50,
    favicon: 0,
    selectptPrice: 0,
    propertyChildIds: "",
    propertyChildNames: "",
    canSubmit: false, //  选中规格尺寸时候是否允许加入购物车
    shopCarInfo: {},
    shopType: "addShopCar",//购物类型，加入购物车或立即购买，默认为加入购物车
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0
    },
    wxlogin: true,
    sharecode: true,
    sharebox: true,
		title:"商品详情",
    barBg: 'red',
		color: '#ffffff',
    hideCouponPopup: true,
    couponLists:[],
    groupLists:[]
  },
  // 点击放大图片
  preview(e){
    let currentUrl = e.currentTarget.dataset.src
      wx.previewImage({
        current: currentUrl, // 当前显示图片的http链接
        urls: this.data.goodsDetail.thumbnail_img // 需要预览的图片http链接列表
      })
  },
  previewDetail(e){
    let currentUrl = e.currentTarget.dataset.src
      wx.previewImage({
        current: currentUrl, // 当前显示图片的http链接
        urls: this.data.goodsDetail.image_detail // 需要预览的图片http链接列表
      })
  },
  // 复制文本
  copyText(e){
    util.copyText(e.currentTarget.dataset.content);
  },
  //事件处理函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  // 语言 
  // 设置language变量（翻译Object）
  setLanguage() {
    var lang = wx.T.getLanguage()
    this.setData({
      language: lang,
      selectSize: lang.select_attribute
    });
  },
  changeLanguage() {
    var lang = wx.T.getLanguage()
    this.setData({
      language: lang,
      selectSize: lang.select_attribute
    });
    this.getProductDetails()
  },
  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        let titleBarHeight=44;
        if (res.model.indexOf('iPhone') !== -1) {
          titleBarHeight= 44
        } else {
          titleBarHeight = 48
        }
        that.setData({
          statusBarHeight: res.statusBarHeight,
          titleBarHeight: titleBarHeight
        });
      },
      failure() {
        that.setData({
          statusBarHeight: 0,
          titleBarHeight: 0
        });
      }
    })

    setTimeout(function () {
      if (app.globalData.usinfo == 0) {
        that.setData({
          wxlogin: false
        })
      }
      //that.goPingtuan();
      //that.goPingList();
    }, 1000)
    // 李珊珊
    that.getProductDetails();
  },
  headerBack(){
    if(this.data.share==1){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }else{
      wx.navigateBack({
        delta: 1,
      })
    }
  },
  onLoad: function (e) {
     // 带上这个数据
     if(e.getBdmin_user_id){
       util.clearLogin(e.getBdmin_user_id);
    }
    this.setData({
      share:e.share,
      is_live:e.is_live||2
    })
    // 分享进来的
    if(e.is_live==1){
      util.stat(app,'pages/goods-detail/goods-detail',3)
    }else if(e.share==1){
      util.stat(app,'pages/goods-detail/goods-detail',2)
    }else{
      util.stat(app,'pages/goods-detail/goods-detail',1)
    }
    var that = this;
    // 用户是否登录
    // 语言
    // 设置当前页面的language变量 - 每个页面都要有
    this.setLanguage();
    event.on("languageChanged", this, this.changeLanguage); // (2)
    // 设置当前页面的language Index - 每个页面都要有
    wx.T.setLocaleByIndex(wx.T.langIndex);
    // 语言 - 结束
    
    var value = wx.getStorageSync('userInfo')
      if (!e.id) { //扫码进入
        var scene = decodeURIComponent(e.scene);
        if (scene.length > 0 && scene != undefined) {
          var scarr = scene.split(',');
          var dilist = [];
          for (var i = 0; i < scarr.length; i++) {
            dilist.push(scarr[i].split('='))
          }
          if (dilist.length > 0) {
            var dict = {};
            for (var j = 0; j < dilist.length; j++) {
              dict[dilist[j][0]] = dilist[j][1]
            }
            var id = dict.i;
            var vid = dict.u;
            var sid = dict.s;
            that.setData({
              id: id
            })
            if (vid) {
              wx.setStorage({
                key: 'inviter_id_' + id,
                data: vid
              })
            }
            if (sid) { that.setData({ share: sid }); }
          }
        }
      }
      if (!e.scene) { //链接进入
        if (e.inviter_id) {
          wx.setStorage({
            key: 'inviter_id_' + e.id,
            data: e.inviter_id
          })
        }
        if (e.share) { that.setData({ share: e.share }); }
        that.setData({
          id: e.id
        })
      }
      if (app.globalData.iphone == true) { that.setData({ iphone: 'iphone' }) }
      // this.getProductDetails()
      this.getfav();
      // 获取购物车数据
      wx.getStorage({
        key: 'shopCarInfo',
        success: function (res) {
          //console.log("shopCarInfo>>>")
          //console.log(res.data)
          that.setData({
            shopCarInfo: res.data,
            shopNum: res.data.shopNum
          });
        }
      })
      // 获取优惠券列表
      this.getCouponLists();
      // 获取拼团列表
      this.getGroupCoupon();
      // 添加店铺浏览记录
      util.storeBrower(app)

  },
  // 获取拼团列表
  getGroupCoupon(){
    var that = this
    wx.request({
      url: app.globalData.urls + '/product/group',
      header: app.getRequestHeader(),
      data: {
        product_id: that.data.id
      },
      success(res) {
        let data = res.data.data,status=res.data.code;
        if(status==200){
          let result = data.map(val=>{
            val.time = util.sec_to_time(val.limit_time) 
            return val;
          })
          
          that.setData({
            groupLists: result
          })
          that.countDown();
        }else{
          that.setData({
            groupLists: []
          })
        }
        app.saveReponseHeader(res);
      }
    });
  },
  // 倒计时
  countDown(val){
    let that = this;
    setInterval(()=>{
      let result = that.data.groupLists.map(val => {
        if (val.limit_time>=1) val.limit_time--;
        val.time = util.sec_to_time(val.limit_time)
        return val;
      })
      that.setData({
        groupLists: result
      }) 
    },800)
  },
 
  getProductDetails: function() {
    var that = this
    let customer_id = wx.getStorageSync('customer_id')
    wx.request({
      url: app.globalData.urls + '/catalog/product/index',  // '/shop/goods/detail',
      header: app.getRequestHeader(),
      data: {
        product_id: that.data.id,
        customer_id:customer_id.customer_id
      },
      success: function (res) {
        let userinfo = wx.getStorageSync('userInfo'),customer_id = wx.getStorageSync('customer_id');
        let code = res.data.code
        if(code!=200) {
          if(code==1300001){
            wx.showModal({
              title: "404 没找到商品",
              content:"商品不存在或已下架",
              showCancel: false,
              success(res){
                // 返回上一层
                wx.navigateBack()
              }
            })
          }else{
            if(!userinfo || !customer_id){
              util.loginAccount(app,function(){
                that.onShow()
              });
            }
          }
          
          return;
        };
        var selectSizeTemp = "";
        var goodsDetail = res.data.data.product;
        if (!goodsDetail) {
          wx.showModal({
            title: that.data.language.error,
            content: res.data.message,
            showCancel: false,
            success(res){
              // 返回上一层
              wx.navigateBack()
            }
          })
          return
        }
        var is_custom_option_empty = true;
        // 判断是否为空
        if (goodsDetail.custom_option) {
          for (var x in goodsDetail.custom_option) {
            is_custom_option_empty = false;
          }
        }
        
        if (!is_custom_option_empty || goodsDetail.options.length != 0) {
          for (var i = 0; i < goodsDetail.options.length; i++) {
            selectSizeTemp = selectSizeTemp + " " + goodsDetail.options[i].label;
          }
          that.setData({
            hasMoreSelect: true,
            selectSize: that.data.selectSize + selectSizeTemp,
            // selectSizePrice: res.data.data.product.price_info.special_price, // 该设定与下6行重复
            selectptPrice: res.data.data.product.price_info.price
          });
        };

        var goodsDetail= res.data.data.product;

        goodsDetail.singleBuy = (goodsDetail.price_info.price.value * 1.3).toFixed(2);
        
        that.setData({
          goodsDetail: goodsDetail,
          selectSizePrice: goodsDetail.price_info.special_price ? goodsDetail.price_info.special_price:0,
          buyNumber: 1,  //(res.data.data.basicInfo.stores > 0) ? 1 : 0,
        });
        WxParse.wxParse('article', 'html', res.data.data.product.description, that, 5);
        app.saveReponseHeader(res);
      }
    });
  },
  
  goShopCar: function () {
    wx.reLaunch({
      url: "/pages/cart/cart"
    });
  },
  // 去参团
  gobuy(e) {
    wx.navigateTo({
      url: "/pages/pay-order/pay-order?orderType=buyPT&groupid=" + e.target.dataset.id + "&product_info=" + JSON.stringify([{ product_id: this.data.id, qty: 1 }]) + "&buy_type=1&is_live="+this.data.is_live
    })
  },
  // 添加到购物车
  toAddShopCar: function () {
    
    this.setData({
      shopType: "addShopCar"
    })
    this.bindGuiGeTap();
  },
  // 单独开团
  singleBuy(){
    this.setData({
      shopType: "group"
    });
    // this.bindGuiGeTap();
    this.buyNow();
  },
  // 单独购买
  tobuy: function () {
    this.setData({
      shopType: "tobuy"
    });
    // this.bindGuiGeTap();
    this.buyNow();
  },
  
  /**
   * 规格选择弹出框
   */
  bindGuiGeTap: function () {
    this.setData({
      hideShopPopup: false
    })
  },
  /**
   * 规格选择弹出框隐藏
   */
  closePopupTap: function () {
    this.setData({
      hideShopPopup: true
    })
  },
  /**
   * 优惠券弹出框显示
   */
  showCoupon: function () {
    let customer_id = wx.getStorageSync('customer_id')
    if(!customer_id){
      wx.navigateTo({
         url: '/pages/login/login',
      })
      return;
    }
    this.setData({
      hideCouponPopup: false
    })
  },
  /**
   * 优惠券弹出框隐藏
   */
  closeCouponTap: function () {
    this.setData({
      hideCouponPopup: true
    })
  },
  /**
   * 优惠券弹出框隐藏
   */
  getCouponLists: function () {
    var that=this;
    wx.request({
      url: app.siteInfo.url + app.siteInfo.subDomain + '/coupon/coupon?product_id=' + this.data.id+'&bdmin_user_id='+util.share_bdmin_user_id(),//+'&store_id=3',--暂时没有店铺id
      data: {},
      success: function (res) {
        if(res.data.code==200){
          that.setData({
            couponLists: res.data.data
          })
        }
      }
    })
  },
  /**
   * 领取优惠券
   */
  receiveCouponTap(e){
    var that = this,customer_id = wx.getStorageSync('customer_id');
    wx.request({
      url: app.siteInfo.url + app.siteInfo.subDomain + '/coupon/add?coupon_id=' + e.currentTarget.dataset.id +'&customer_id='+customer_id.customer_id,
      data: {},
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            icon: "none",
            title: '领取成功',
          })
        }else{
          wx.showToast({
            icon: "none",
            title: res.data.message,
          })
        }
      }
    })
  },
  numJianTap: function () {
    if (this.data.buyNumber > this.data.buyNumMin) {
      var currentNum = this.data.buyNumber;
      currentNum--;
      this.setData({
        buyNumber: currentNum
      })
    }
  },
  numJiaTap: function () {
    if (this.data.buyNumber < this.data.buyNumMax) {
      var currentNum = this.data.buyNumber;
      currentNum++;
      this.setData({
        buyNumber: currentNum
      })
    }
  },
  // options 跳转
  labelOptionsItemTap: function (event) {
    var optionid = event.currentTarget.dataset.optionid
    this.setData({
      id: optionid,
      selectSize: this.data.language.select_attribute
    })
    this.getProductDetails()
    //wx.navigateTo({
    //  url: '/pages/goods-detail/goods-detail?id=' + optionid
    //})

  },

  /**
   * 选择商品规格
   * @param {Object} e
   */
  labelItemTap: function (e) {
    var that = this;
    // 取消该分类下的子栏目所有的选中状态
    var childs = that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods;
    for (var i = 0; i < childs.length; i++) {
      that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods[i].active = false;
    }
    // 设置当前选中状态
    that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods[e.currentTarget.dataset.propertychildindex].active = true;
    // 获取所有的选中规格尺寸数据
    var needSelectNum = that.data.goodsDetail.properties.length;
    var curSelectNum = 0;
    var propertyChildIds = "";
    var propertyChildNames = "";
    for (var i = 0; i < that.data.goodsDetail.properties.length; i++) {
      childs = that.data.goodsDetail.properties[i].childsCurGoods;
      for (var j = 0; j < childs.length; j++) {
        if (childs[j].active) {
          curSelectNum++;
          propertyChildIds = propertyChildIds + that.data.goodsDetail.properties[i].id + ":" + childs[j].id + ",";
          propertyChildNames = propertyChildNames + that.data.goodsDetail.properties[i].name + ":" + childs[j].name + "  ";
        }
      }
    }
    var canSubmit = false;
    if (needSelectNum == curSelectNum) {
      canSubmit = true;
    }
    // 计算当前价格
    if (canSubmit) {
      wx.request({
        url: app.siteInfo.url + app.siteInfo.subDomain + '/shop/goods/price',
        data: {
          goodsId: that.data.goodsDetail.basicInfo.id,
          propertyChildIds: propertyChildIds
        },
        success: function (res) {

          that.setData({
            selectSizePrice: res.data.data.price,
            propertyChildIds: propertyChildIds,
            propertyChildNames: propertyChildNames,
            buyNumMax: res.data.data.stores,
            buyNumber: (res.data.data.stores > 0) ? 1 : 0,
            selectptPrice: res.data.data.pingtuanPrice
          });
        }
      })
    }

    this.setData({
      goodsDetail: that.data.goodsDetail,
      canSubmit: canSubmit
    })

  },
  gohone(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  /**
  * 加入购物车
  */
  addShopCar: function () {
    let customer_id = wx.getStorageSync('customer_id')
    if(!customer_id){
      wx.navigateTo({
         url: '/pages/login/login',
      })
      return;
    }
    var that = this
    if (that.data.buyNumber < 1) {
      wx.showModal({
        title: that.data.language.tips,
        content: that.data.language.buy_amount_min + "1" + that.data.language.item,
        showCancel: false
      })
      return;
    }
    
    // wx.showLoading({
    //   title: 'loading...',
    // })
    var requestHeader= app.getRequestHeader();
    requestHeader['Content-Type'] = 'application/x-www-form-urlencoded';
    wx.request({
      url: app.globalData.urls + '/cart/add', // /checkout
      method: 'POST',
      header: requestHeader,
      data: {
        qty: that.data.buyNumber,
        product_id: that.data.goodsDetail._id,
        customer_id: util.getCustomerId()
      },
      success: function (res) {
        if (res.data.code == 200) {
          var qty = res.data.data.items_count;
          var shopCarInfo = {}
          shopCarInfo.shopNum = qty
          that.setData({
            shopNum: qty,
          })
          wx.setStorage({
            key: "shopCarInfo",
            data: shopCarInfo
          })
          app.getShopCartNum()
          //that.bindGuiGeTap();
          wx.showToast({
            title: that.data.language.add_to_cart_success ,
            icon: 'success',
            duration: 2000
          });
          that.closePopupTap();
        } else if (res.data.code == 1100003) {
          util.loginAccount(app,function(){
            that.onShow()
          });
        } else {
          wx.showModal({
            title: that.data.language.error,
            content: that.data.language.add_to_cart_fail,
            showCancel: false
          })
        }
      }
    })
  },
	/**
	  * 立即购买
	  */
  buyNow: function () {
    var that = this
    if (that.data.buyNumber < 1) {
      wx.showModal({
        title: that.data.language.tips,
        content: that.data.language.buy_amount_min + "1" + that.data.language.item,
        showCancel: false
      })
      return;
    }
    if (that.data.shopType =="tobuy"){
      wx.navigateTo({
        url: "/pages/pay-order/pay-order?orderType=buyPT&product_info=" + JSON.stringify([{ product_id: that.data.id, qty: that.data.buyNumber }]) +"&buy_type=2&is_live="+that.data.is_live
      })
    }else{
      wx.navigateTo({
        url: "/pages/pay-order/pay-order?orderType=buyPT&product_info=" + JSON.stringify([{ product_id: that.data.id, qty: that.data.buyNumber }]) + "&buy_type=1&is_live="+that.data.is_live
      })
    }
    // 原有的立即购买逻辑
    // wx.showLoading({
    //   title: 'loading...',
    // })
    // var requestHeader = app.getRequestHeader();
    // requestHeader['Content-Type'] = 'application/x-www-form-urlencoded';
    // wx.request({
    //   url: app.globalData.urls + '/cart/add',//checkout
    //   method: 'POST',
    //   header: requestHeader,
    //   data: {
    //     qty: that.data.buyNumber,
    //     buy_now: 1,
    //     product_id: that.data.goodsDetail._id,
    //     customer_id: util.getCustomerId()
    //   },
    //   success: function (res) {
    //     if (res.data.code == 200) {
    //       var qty = res.data.data.items_count;
    //       var shopCarInfo = {}
    //       shopCarInfo.shopNum = qty
    //       that.setData({
    //         shopNum: qty,
    //       })
    //       wx.setStorage({
    //         key: "shopCarInfo",
    //         data: shopCarInfo
    //       })
    //       console.log(shopCarInfo)
    //       app.getShopCartNum()
    //       //that.bindGuiGeTap();
    //       wx.showToast({
    //         title: that.data.language.add_to_cart_success,
    //         icon: 'success',
    //         duration: 2000
    //       });
    //       that.closePopupTap();
    //       wx.navigateTo({
    //         url: "/pages/pay-order/pay-order?orderType=buyPT"
    //       })
    //     } else if (res.data.code == 1100003) {
    //       wx.navigateTo({
    //         url: "/pages/login/login"
    //       })
    //     } else {
    //       wx.showModal({
    //         title: that.data.language.error,
    //         content: that.data.language.add_to_cart_fail,
    //         showCancel: false
    //       })
    //     }
    //   }
    // }) 
  },
  
	/**
	 * 组建立即购买信息
	 */
  buliduBuyNowInfo: function () {
    var shopCarMap = {};
    shopCarMap.goodsId = this.data.goodsDetail.basicInfo.id;
    shopCarMap.pic = this.data.goodsDetail.basicInfo.pic;
    shopCarMap.name = this.data.goodsDetail.basicInfo.name;
    // shopCarMap.label=this.data.goodsDetail.basicInfo.id; 规格尺寸 
    shopCarMap.propertyChildIds = this.data.propertyChildIds;
    shopCarMap.label = this.data.propertyChildNames;
    shopCarMap.price = this.data.selectSizePrice;
    shopCarMap.left = "";
    shopCarMap.active = true;
    shopCarMap.number = this.data.buyNumber;
    shopCarMap.logisticsType = this.data.goodsDetail.basicInfo.logisticsId;
    shopCarMap.logistics = this.data.goodsDetail.logistics;
    shopCarMap.weight = this.data.goodsDetail.basicInfo.weight;

    var buyNowInfo = {};
    if (!buyNowInfo.shopNum) {
      buyNowInfo.shopNum = 0;
    }
    if (!buyNowInfo.shopList) {
      buyNowInfo.shopList = [];
    }
    buyNowInfo.shopList.push(shopCarMap);
    return buyNowInfo;
  },
  bulidupingTuanInfo: function () {
    var shopCarMap = {};
    shopCarMap.goodsId = this.data.goodsDetail.basicInfo.id;
    shopCarMap.pingtuanId = this.data.pingtuanOpenId;
    shopCarMap.pic = this.data.goodsDetail.basicInfo.pic;
    shopCarMap.name = this.data.goodsDetail.basicInfo.name;
    // shopCarMap.label=this.data.goodsDetail.basicInfo.id; 规格尺寸 
    shopCarMap.propertyChildIds = this.data.propertyChildIds;
    shopCarMap.label = this.data.propertyChildNames;
    shopCarMap.price = this.data.selectptPrice;
    //this.data.goodsDetail.basicInfo.pingtuanPrice;
    shopCarMap.left = "";
    shopCarMap.active = true;
    shopCarMap.number = this.data.buyNumber;
    shopCarMap.logisticsType = this.data.goodsDetail.basicInfo.logisticsId;
    shopCarMap.logistics = this.data.goodsDetail.logistics;
    shopCarMap.weight = this.data.goodsDetail.basicInfo.weight;

    var buyNowInfo = {};
    if (!buyNowInfo.shopNum) {
      buyNowInfo.shopNum = 0;
    }
    if (!buyNowInfo.shopList) {
      buyNowInfo.shopList = [];
    }
    buyNowInfo.shopList.push(shopCarMap);
    return buyNowInfo;
  },

  onShareAppMessage:async function () {
    let that=this;
 
    that.setData({ sharebox: true })
    var share_img = await new Promise((resolve,reject)=>{
      wx.request({
        url: app.globalData.urls + '/product/share-image',
        method: 'POST',
        data: {
          price:this.data.goodsDetail.price_info.price.value,
          show_price:this.data.goodsDetail.show_price,
          product_id: this.data.id,
          img_url:this.data.goodsDetail.main_image
        },
        header: app.getRequestHeader(),
        success(res) {
          let share_img=res.data.data
          resolve(share_img.share_img)
        }
      })
    })

    console.log('------------',share_img)
    return {
      title: that.data.goodsDetail.name,
      desc:that.data.goodsDetail.name,
      imageUrl:share_img,//share_img.share_img ,  //用户分享出去的自定义图片大小为5:4,
      path: '/pages/goods-detail/goods-detail?id=' + that.data.goodsDetail._id + '&inviter_id=' + app.globalData.uid + '&share=1&getBdmin_user_id='+util.share_bdmin_user_id(),
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon:'success'
        })
        // 转发成功
      },
      fail: function (res) {
        wx.showToast({
          title: '转发失败',
        })
        // 转发失败
      }
    }
  },
  
  getfav: function () {
    //console.log(e)
    var that = this;
    var id = that.data.id
    wx.request({
      url: app.globalData.urls + '/catalog/product/getfav',
      data: {
        //nameLike: this.data.goodsDetail.basicInfo.name,
        //token: app.globalData.token
        product_id: id,
      },
      header: app.getRequestHeader(),
      success: function (res) {
        if (res.data.code == 200) {
          if (res.data.data.fav == 1) {
            that.setData({
              favicon: 1
            });
          }
        }
      }
    })
  },
  fav: function () {
    let customer_id = wx.getStorageSync('customer_id')
    if(!customer_id){
      wx.navigateTo({
         url: '/pages/login/login',
      })
      return;
    }
    var that = this;
    wx.request({
      url: app.globalData.urls + '/catalog/product/favorite',
      data: {
        bdmin_user_id:util.share_bdmin_user_id(),
        product_id: that.data.id,
        type: 'add',
        //token: app.globalData.token
      },
      header: app.getRequestHeader(),
      success: function (res) {
        if (res.data.code == '200') {
          /*
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            image: '../../images/active.png',
            duration: 2000
          })
          */
          wx.showToast({
            title: that.data.language.add_to_collection_success,
            duration: 2000
          })
          that.setData({
            favicon: 1
          });
        } else if (res.data.code == '1100003') {
          // 需要登陆
          util.loginAccount(app,function(){
            that.onShow()
          });
        }
        app.saveReponseHeader(res);
      }
    })
  },
  del: function () {
    var that = this;
    wx.request({
      url: app.globalData.urls + '/catalog/product/favorite',
      data: {
        product_id: that.data.id,
        type: 'del',
        //token: app.globalData.token
      },
      header: app.getRequestHeader(),
      success: function (res) {
        if (res.data.code == 200) {
          /*
          wx.showToast({
            title: '取消收藏',
            icon: 'success',
            image: '../../images/error.png',
            duration: 2000
          })
          */
          wx.showToast({
            title: that.data.language.remove_for_collection_success,
            duration: 2000
          })

          that.setData({
            favicon: 0
          });
        } else if(res.data.code == '1100003') {
          // 需要登陆
          util.loginAccount(app,function(){
            that.onShow()
          });
        }
        app.saveReponseHeader(res);
      }
    })
  },
  
  gohome: function () {
    wx.switchTab({
      url: "/pages/index/index"
    })
  },
  tabFun: function (e) {
    var _datasetId = e.target.dataset.id;
    var _obj = {};
    _obj.curHdIndex = _datasetId;
    _obj.curBdIndex = _datasetId;
    this.setData({
      tabArr: _obj
    });
  },
  
  
  getShareBox:function(){
    this.setData({sharebox: false})
  },
  getcode: function () {
    var that = this;
    wx.showLoading({
      title: that.data.language.generating,
    })
    var product_id = that.data.goodsDetail._id
    wx.request({
      url: app.globalData.urls + '/wx/helper/qrcode',
      data: {
        scene: "i=" + product_id + ",u=" + app.globalData.uid + ",s=1",
        page: "pages/goods-detail/goods-detail",
        expireHours:1
      },
      success: function (res) {
        if (res.data.code == 0) {
          wx.downloadFile({
            url: res.data.data,
            success: function (res) {
              wx.hideLoading()
              that.setData({
                codeimg: res.tempFilePath,
                sharecode: false,
                sharebox: true
              });
            }
          })
        }
      }
    });
  },
  savecode: function () {
    var that = this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.codeimg,
      success(res) {
        wx.showToast({
          title: that.data.language.save_success,
          icon: 'success',
          duration: 2000
        })
      }
    })
    that.setData({
      sharecode: true,
    })
  },
  closeshare: function () {
    this.setData({
      sharebox: true,
      sharecode: true
    })
  },
  gotoGoodsEval(){
    
    wx.navigateTo({
      url: "/pages/evaluate/evaluate?id=" + this.options.id
    })
  },
  totalViews(e){
    this.setData({
      totals: e.detail
    })
  },
})
